package com.sunronsoft.mind.map;

import com.alibaba.fastjson.JSONArray;
import com.jfinal.kit.*;
import com.sunronsoft.mind.map.model.User;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Sunron
 **/
public class UserService {

    private static UserService service;
    private UserService() {}
    
    public static UserService getInstance() {
        if (null == service) service = new UserService();
        return service;
    }
    
    public String dataFile() {
        String path = PathKit.getWebRootPath() + "/data/";
        DataKit.processDirectory(path);
        
        return path + MainConfig.prop.get("userData");
    }
    
    public void init() {
        User user = new User(MainConfig.prop.get("initUserName"), MainConfig.prop.get("initPassword"), false);
        
        List<User> users = new ArrayList<>();
        users.add(user);

        String fileName = this.dataFile();
        boolean success = DataKit.writeJsonFile(fileName, users.toString());
        LogKit.info("初始化管理员完成");
    }

    public boolean save(String userName, String password) {
        User user = new User(userName, HashKit.md5(password));
        
        List<User> users = this.list();
        users.removeIf(item -> userName.equalsIgnoreCase(item.getUserName()));
        users.add(user);

        String fileName = this.dataFile();
        boolean success = DataKit.writeJsonFile(fileName, users.toString());
        LogKit.info(String.format("保存管理员数据：%s", user.toString()));
        return success;
    }
    
    public boolean delete(String userName) {
        boolean success = true;
        List<User> users = this.list();
        if (users.removeIf(item -> userName.equalsIgnoreCase(item.getUserName()))) {
            String fileName = this.dataFile();
            success = DataKit.writeJsonFile(fileName, users.toString());
            LogKit.info(String.format("删除管理员数据：%s", userName));
        }
        return success;
    }
    
    public Kv login(String userName, String password) {
        List<User> users = this.list();
        List<User> result = users.stream()
                .filter(item -> userName.equalsIgnoreCase(item.getUserName())
                                && password.equals(item.getPassword())
                ).collect(Collectors.toList());
        
        Kv user = Kv.create();
        if (!result.isEmpty()) {
            user.set("userName", result.get(0).getUserName());
            user.set("token", StrKit.getRandomUUID());
            LogKit.info(String.format("管理员：%s登录成功", result.get(0).getUserName()));
        }
        
        return user;
    }
    
    public User detail(String userName) {
        List<User> users = this.list();
        List<User> result = users.stream()
                .filter(item -> userName.equalsIgnoreCase(item.getUserName()))
                .collect(Collectors.toList());
        if (!result.isEmpty()) {
            return result.get(0);
        } else {
            return null;
        }
    }
    
    public List<User> list() {
        String fileName = this.dataFile();
        String userStr = DataKit.readJsonFile(fileName);
        
        List<User> users = JSONArray.parseArray(userStr, User.class);
        if (null == users) {
            return new ArrayList<>();
        } else {
            return users;
        }
    }
}
